package com.taplinker.client.im.activity.message;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.taplinker.client.AppConfigContext;
import com.taplinker.client.R;
import com.taplinker.client.core.ImageManager;
import com.taplinker.client.im.protocol.TapLinkerMessage;
import com.taplinker.client.im.util.SmileyParser;
import com.taplinker.core.util.ImageUtil;
import com.taplinker.core.util.TimeUtil;

public class RightTextMessage extends AbstractMessageView{

	@Override
	public View getMessageView(Context context, TapLinkerMessage message, View convertView) {
		TextMessageHolder textMessageHolder = null;
		if(convertView == null) {
			convertView = LayoutInflater.from(context).inflate(MessageTypeEnum.getMessageType(message).getLayoutId(), null);
			textMessageHolder = new TextMessageHolder();
			textMessageHolder.name = (TextView) convertView.findViewById(R.id.name_text_tv);
			textMessageHolder.head = (ImageView) convertView.findViewById(R.id.head_icon_iv);
			textMessageHolder.content = (TextView) convertView.findViewById(R.id.content_text_tv);
			textMessageHolder.time = (TextView) convertView.findViewById(R.id.time_text_tv);
			textMessageHolder.state = (TextView) convertView.findViewById(R.id.sender_state_tv);
			convertView.setTag(textMessageHolder);
		} else {
			textMessageHolder = (TextMessageHolder) convertView.getTag();
		}
		
		textMessageHolder.name.setText(message.getSendUser());
		if(!TextUtils.isEmpty(message.getIcon())) {
			String headUrl = AppConfigContext.WEB_DISK_ROOT + message.getIcon();
			ImageManager.getInstance().loadImage(headUrl, textMessageHolder.head, 45, 45,null);
		}
		SmileyParser parser = new SmileyParser(context);
		textMessageHolder.content.setText(parser.replace(message.getContent()));
		textMessageHolder.content.setOnLongClickListener(new MessageOnLongClickListener(message, context));
		textMessageHolder.state.setText("");
		textMessageHolder.state.setPadding(
				ImageUtil.dip2px(context, 5),
				ImageUtil.dip2px(context, 3),
				ImageUtil.dip2px(context, 5),
				ImageUtil.dip2px(context, 3));
		textMessageHolder.state.setBackgroundResource(R.drawable.message_state_bg);
		if (message.isUnarrived()){
			// 切换未送到消息图标提示
			textMessageHolder.state.setTextColor(Color.RED);
			textMessageHolder.state.setPadding(0, 0, 0, 0);
			textMessageHolder.state.setBackgroundResource(R.drawable.msg_warning);
		} else if (message.getSendTime() == null) {
			//textMessageHolder.state.setText("发送中");
		//	textMessageHolder.state.setTextColor(0xff4dae4b);
		} else if (message.getRead()) {
		//	textMessageHolder.state.setText("已读");
			textMessageHolder.state.setBackgroundResource(R.drawable.input_read);
			//textMessageHolder.state.setTextColor(0xff4dae4b);
		} else {
//			textMessageHolder.state.setText("已送达");
//			textMessageHolder.state.setTextColor(0xffff8a00);
			textMessageHolder.state.setBackgroundResource(R.drawable.input_songda);
		}
		if(message.getSendTime() != null) {
			textMessageHolder.time.setText(TimeUtil.timeFormat(message.getSendTime()));
		}
		return convertView;
	}
	
	class TextMessageHolder {
		TextView name;		// 用户名
		ImageView head;		// 头像
		TextView content;	// 内容
		TextView time;		// 时间
		TextView state;		// 发送状态
	}

}